# ------------------------------------------------------------------------------
# Creates df of subsample flags used in analysis
# Updates author: Cassidy Shubatt <cshubatt@gmail.com>
# To run: bash 14_build_subsample_flags.sh
# ------------------------------------------------------------------------------

# Libraries --------------------------------------------------------------------
library(here)
library(yaml)
library(data.table)
library(tidyverse)
library(glue)

u <- modules::use(here("lib", "util.R"))

# Load Data --------------------------------------------------------------------
message("Loading data...")
paths <- read_yaml(here("lib", "filepaths.yml"))
overnight_lab <- ""
disch_dx <- readRDS(paths$analysis$disch_dx)
cohort <- readRDS(glue(paths$analysis$full_cohort)) %>%
  u$safe_left_join(disch_dx)

# Build subsample flags --------------------------------------------------------
message("Building subsample flags...")
flagged_df <- cohort %>%
  mutate(
    full = TRUE,
    noecg = !has_ecg,
    not_sameday_tn = tn_group_sameday == "None",
    not_admit_sym = !undx_admit
  ) %>%
  select(ptid, ed_enc_id, full, noecg, not_sameday_tn, not_admit_sym)

# Save -------------------------------------------------------------------------
message("Saving...")
write_rds(flagged_df, paths$analysis$subsample_flags)

message("Done.")
